package com.luyu.util;

import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class ResponseUtil {

    private static ObjectMapper mapper;

    @Autowired
    public  void setMapper(ObjectMapper mapper) {
        ResponseUtil.mapper = mapper;
    }

    public  static void returnErrorMsg(HttpServletResponse response, Object obj) throws IOException {
        response.setContentType("text/plain; charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.getWriter().write(mapper.writeValueAsString(obj));
    }
}
